<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    width="500px"
    @callBack="onCallBack"
    @close="onClose"
    @open="handleQuery"
  >
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" label-position="left">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" clearable placeholder="请输入名称"/>
      </el-form-item>

      <el-form-item label="编码" prop="code">
        <el-input v-model="form.code" clearable placeholder="请输入编码"/>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="0">页面权限</el-radio>
          <el-radio :label="1">按钮权限</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="上级类别" prop="parentIds">
        <el-select ref="selectTree" v-model="form.parentName" class="main-select-tree">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" style="display: none;"/>
          <el-tree
            ref="selecteltree"
            class="main-select-el-tree"
            :data="options"
            node-key="id"
            highlight-current
            :props="defaultProps"
            :current-node-key="form.parentId"
            :expand-on-click-node="expandOnClickNode"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </el-select>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getOne, getTree, save } from '@/api/sysPermission'

export default {
  data() {
    return {
      dialogVisible: false,
      title: '添加',
      options: [],
      expandOnClickNode: true,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      form: {
        id: '',
        name: '',
        code: '',
        parentId: '',
        parentName: '',
        type: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入编码',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleQuery() {
      getTree().then(res => {
        if (res.code === 20000) {
          this.options = res.data
        }
      })
    },
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data
            this.form.id = rtn.id
            this.form.name = rtn.name
            this.form.code = rtn.code
            this.form.parentId = rtn.parentId
            this.form.parentName = rtn.parentName
            this.form.type = rtn.type
          }
        })
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id
        this.title = '编辑'
        this.init()
      } else {
        this.title = '添加'
      }
      this.dialogVisible = true
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          save(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.onCallBack()
              }
            })
            .catch((e) => {
            })
        } else {
          return false
        }
      })
    },
    handleNodeClick(node) {
      this.form.parentId = node.id
      this.form.parentName = node.name
      this.$refs.selectTree.blur()
    },
    onCallBack() {
      this.$emit('onCallBack')
    }
  }
}
</script>

<style scoped>
.el-form {
  padding: 20px 20px;
}

.main-select-el-tree .el-tree-node .is-current > .el-tree-node__content {
  font-weight: bold;
  color: #409eff;
}

.main-select-el-tree .el-tree-node.is-current > .el-tree-node__content {
  font-weight: bold;
  color: #409eff;
}
</style>
